package com.xzp.bams.server;

import com.xzp.bams.entity.Account;
import com.xzp.bams.exception.*;
import com.xzp.bams.service.Bank;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;

/**
 * @TODO
 * @Author xu
 * 2025/4/13
 */
public class ServerTasker  implements Runnable{

    private Bank bank;
    private Socket socket;

    public ServerTasker(Bank bank , Socket socket){
        this.bank = bank;
        this.socket = socket;
    }
    @Override
    public void run() {

        try (
                //读指令
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                //写结果
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        ){
            String line = null;
            Account acc = null;

            //2.等待读数据
            while ((line=br.readLine())!=null){
                System.out.println("line = "+line);
                //解析指令
                String[] splits = line.split("#");
                switch (splits[0]){
                    case "login":   //登录
                        try {
                            acc = bank.login(Long.valueOf(splits[1]) , splits[2]);
                            if(acc!=null){
                                //3.写回结果  成功则写回Account信息给客户端
                                oos.writeObject(acc);
                                oos.flush();
                            }
                        } catch (NumberFormatException e) {
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject("登录账号不合法");
                            oos.flush();
                        } catch (LoginException e) {
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        break;
                    case "register":
                        try {
                            acc = bank.register(splits[1],splits[2],splits[3],splits[4],splits[5],Integer.parseInt(splits[6]));
                            if(acc!=null){
                                //3.写回结果  成功则写回Account信息给客户端
                                oos.writeObject(acc);
                                oos.flush();
                            }
                        }catch (NumberFormatException e) {
                        //3.写回结果  失败则写回String异常信息给客户端
                        oos.writeObject("登录账号不合法");
                        oos.flush();
                    } catch (RegisterException | TypeException | QueryException e) {
                        //3.写回结果  失败则写回String异常信息给客户端
                        oos.writeObject(e.getMessage());
                        oos.flush();
                    }
                        break;
                    case "deposit":
                        try {
                            acc = bank.deposit(Long.valueOf(splits[1]),Double.parseDouble(splits[2]));
                            if(acc!=null){
                                //3.写回结果  成功则写回Account信息给客户端
                                oos.writeObject(acc);
                                oos.flush();
                            }
                        } catch (NumberFormatException e){
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject("登录账号不合法");
                            oos.flush();
                        }  catch (QueryException e) {
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        break;
                    case "withdraw":
                        try {
                            acc = bank.withdraw(Long.valueOf(splits[1]),splits[2],Double.parseDouble(splits[3]));
                            if(acc!=null){
                                //3.写回结果  成功则写回Account信息给客户端
                                oos.writeObject(acc);
                                oos.flush();
                            }
                        }catch (NumberFormatException e){
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject("登录账号不合法");
                            oos.flush();
                        }  catch (QueryException | BalanceNotEnoughException e) {
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        break;
                    case "updateCeiling":
                        try {
                            acc = bank.updateCeiling(Long.valueOf(splits[1]),splits[2],Double.parseDouble(splits[3]));
                            if(acc!=null){
                                //3.写回结果  成功则写回Account信息给客户端
                                oos.writeObject(acc);
                                oos.flush();
                            }
                        } catch (NumberFormatException e){
                        //3.写回结果  失败则写回String异常信息给客户端
                        oos.writeObject("登录账号不合法");
                        oos.flush();
                    }  catch (QueryException | TypeException e) {
                        //3.写回结果  失败则写回String异常信息给客户端
                        oos.writeObject(e.getMessage());
                        oos.flush();
                    }
                        break;
                    case "requestLoan":
                        try {
                            acc = bank.requestLoan(Long.valueOf(splits[1]),Double.parseDouble(splits[2]));
                            if(acc!=null){
                                //3.写回结果  成功则写回Account信息给客户端
                                oos.writeObject(acc);
                                oos.flush();
                            }
                        }catch (NumberFormatException e){
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject("登录账号不合法");
                            oos.flush();
                        }  catch (QueryException | TypeException | LoanException e) {
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        break;
                    case "payLoan":
                        try {
                            acc = bank.payLoan(Long.valueOf(splits[1]),Double.parseDouble(splits[2]));
                            if(acc!=null){
                                //3.写回结果  成功则写回Account信息给客户端
                                oos.writeObject(acc);
                                oos.flush();
                            }
                        }catch (NumberFormatException e){
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject("登录账号不合法");
                            oos.flush();
                        }  catch (QueryException | TypeException | LoanException e) {
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                    case "transfer":
                        //2.解析指令，执行业务操作
                        try {
                            boolean flag = bank.transfer(Long.valueOf(splits[1]),splits[2],Long.valueOf(splits[3]),Double.parseDouble(splits[4]));
                            if(flag){
                                //3.写回结果  成功则写回Account信息给客户端  获取当前账户信息
                                acc = bank.login(Long.valueOf(splits[1]) , splits[2]);
                                if(acc!=null){
                                    oos.writeObject(acc);
                                    oos.flush();
                                }
                            }
                        } catch (NumberFormatException e) {
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject("登录账号不合法");
                            oos.flush();
                        } catch (QueryException | TransferException | BalanceNotEnoughException | LoginException e) {
                            //3.写回结果  失败则写回String异常信息给客户端
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        //3.写结果
                        break;
                    case "close":   //关闭
                        //写回  空响应
                        oos.writeObject("");
                        oos.flush();
                        break;
                    default:
                        break;
                }
            }
        } catch (SocketException e) {

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    }

